package com.xiaoma.config;

import com.rabbitmq.client.Channel;
import com.xiaoma.entity.Payorder;
import constant.RabbitMqConstatnt;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;
import java.util.HashMap;

/**
 * @Author: xiaoma
 * @Date: 2021/7/16 9:45
 * @description:
 */
@Configuration
public class MyMQConfig {
//    @RabbitListener(queues = "order.release.order.queue")
//    public  void  listener(Payorder order, Message message, Channel channel) throws IOException {
//        System.out.println("收到过期消息，准备关闭订单:"+order.getId());
//        //手动ack，非批量模式
//        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
//    }
   @Bean
    public MessageConverter messageConverter(){
        return  new Jackson2JsonMessageConverter();
    }
    //延迟队列
    @Bean
    public Queue orderDelayQueue(){
        HashMap<String, Object> arguments = new HashMap<>();
        //死信交换机
        arguments.put(RabbitMqConstatnt.x_dead_letter_exchange, "order-event-exchange");
        //死信路由键
        arguments.put(RabbitMqConstatnt.x_dead_letter_routing_key, "order.release.order");
        // 消息过期时间 1分钟
        arguments.put(RabbitMqConstatnt.x_message_ttl, RabbitMqConstatnt.ttl_1minute);
        return new Queue("order.delay.queue", // 队列名字
                true, //是否持久化
                false, // 是否排他
                false, // 是否自动删除
                arguments); // 属性map
    }

    //普通队列
    @Bean
    public Queue orderReleaseQueue(){
        return new Queue("order.release.order.queue", // 队列名字
                true, //是否持久化
                false, // 是否排他
                false);  // 是否自动删除
    }

    //交换机
    @Bean
    public Exchange orderEventExchange() {
        return new TopicExchange("order-event-exchange", // name
                true,// durable
                false); // autoDelete
        // 还有一个参数是Map<String, Object> arguments
    }

    @Bean
    public Binding orderCreateBinding() {
        return new Binding("order.delay.queue", // 目的地（队列名或者交换机名字）
                Binding.DestinationType.QUEUE,  // 目的地类型（Queue、Exhcange）
                "order-event-exchange", // 交换器
                "order.create.order", // 路由key
                null); // 参数map
    }
    @Bean
    public Binding orderReleaseBinding() {
        return new Binding("order.release.order.queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.release.order",
                null);
    }
    /**
     * 商品秒杀队列
     */
    @Bean
    public Queue orderSecKillOrrderQueue() {
        Queue queue = new Queue("order.seckill.order.queue", true, false, false);
        return queue;
    }
    @Bean
    public Binding orderSecKillOrrderQueueBinding() {
        //String destination, DestinationType destinationType, String exchange, String routingKey,
        // 			Map<String, Object> arguments
        Binding binding = new Binding(
                "order.seckill.order.queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.seckill.order",
                null);
        return binding;
    }
}
